package mysql

import (
	"box-fresh/hemarpc/hemarpc"
	"gorm.io/gorm"
)

type Hes struct {
	gorm.Model
	Name   string  `gorm:"type:varchar(50);not null;comment:'姓名'"`
	Weight float64 `gorm:"type:decimal(10,2);not null;comment:'重量'"`
	Price  float64 `gorm:"type:decimal(10,2);not null;comment:'价格'"`
}

type Fen struct {
	gorm.Model
	Title string `gorm:"type:varchar(50);not null;comment:'类型'"`
	Hid   int    `gorm:"type:int"`
}
type HesFen struct {
	Fid    int
	Name   string
	Weight float64
	Price  float64
	Title  string
}

func (f *HesFen) HesFenList(db *gorm.DB, lists []*hemarpc.HeList, in *hemarpc.HeListRequest) ([]*hemarpc.HeList, error) {
	err2 := db.Debug().Model(&f).Table("fens").
		Select(
			"fens.id AS Fid",
			"name ",
			"weight ",
			"price ",
			"title ",
		).Joins("LEFT JOIN hes ON hes.id = fens.hid").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&lists).Error
	return lists, err2
}

func (h *Hes) HesAdd(db *gorm.DB) error {
	return db.Debug().Create(h).Error
}

func (h *Hes) HesShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).First(h).Error
}
